﻿using Craftsman.Core.CustomizeException;
using Craftsman.Core.Domain;
using Craftsman.Core.Domain.Repositories;
using Craftsman.xCoronavirus.Domain.Contract;
using Craftsman.xCoronavirus.Domain.Entities;
using Craftsman.xCoronavirus.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Craftsman.xCoronavirus.Domain
{
    public interface IQuestionnaireService : IService
    {
        List<Questionnaire> GetQuestionnaires(QuestionnaireStatus status);
        //List<Questionnaire> GetQuestionnaires(QuestionnaireStatus questionnaireStatus);
        Questionnaire GetQuestionnaire(string id);
        void CreateQuestionnaire(Questionnaire entity);
        void PassQuestionnaire(string id);
        void InvalidQuestionnaire(string id);
        Questionnaire GetQuestionnaireByFormCode(string form_code);
    }
    public class QuestionnaireService : IQuestionnaireService
    {
        private readonly IQuestionnaireRepository _repoQuestionnaire;
        private readonly IHospitalService _hospitalService;

        public QuestionnaireService(
            IQuestionnaireRepository repoQuestionnaire, IHospitalService hospitalService
        )
        {
            _repoQuestionnaire = repoQuestionnaire;
            _hospitalService = hospitalService;
        }
        public void CreateQuestionnaire(Questionnaire entity)
        {
            _repoQuestionnaire.CreatQuestionnaire(entity);
        }

        public Questionnaire GetQuestionnaire(string id)
        {
            var entity = _repoQuestionnaire.GetQuestionnaire(id);
            if (entity == null)
            {
                throw new BusinessException($"不存在对应问卷信息：id=<{id}");
            }
            return entity;
        }

        public List<Questionnaire> GetQuestionnaires(QuestionnaireStatus status)
        {
            return _repoQuestionnaire.GetAllQuestionnaire(status);
        }

        public void InvalidQuestionnaire(string id)
        {
            //此处需要做一些验证
            var entity = _repoQuestionnaire.GetQuestionnaire(id);
            if (entity.Status != QuestionnaireStatus.Draft)
            {
                throw new Exception($"此信息不是未审核状态!");
            }
            entity.Status = QuestionnaireStatus.Invalid;

            //TODO: 此处需要修改医院信息
            //.....
            //TODO: 此处需要修改医院信息

            _repoQuestionnaire.UpdateQuestionnaire(entity);
        }

        /// <summary>
        /// 通过 需要创建医院信息
        /// </summary>
        /// <param name="id"></param>
        public void PassQuestionnaire(string id)
        {
            //此处需要做一些验证
            var entity = _repoQuestionnaire.GetQuestionnaire(id);
            if (entity.Status != QuestionnaireStatus.Draft)
            {
                throw new Exception($"此信息不是未审核状态!");
            }
            entity.Status = QuestionnaireStatus.Pass;

            //TODO: 此处需要修改医院信息
            //.....
            //TODO: 此处需要修改医院信息

            List<Hospital> list = _hospitalService.GetHospitals(entity.Hospital.Name);
            Hospital hospital = list?.FirstOrDefault(l => entity.Hospital.Name == l.Name);
            if (hospital == null)
            {
                Hospital createHospital = BuildData(entity);
                _hospitalService.CreateHospital(createHospital);
            }
            _repoQuestionnaire.UpdateQuestionnaire(entity);

        }

        private Hospital BuildData(Questionnaire entity)
        {
            Hospital createHospital = new Hospital();
            createHospital.Name = entity.Hospital.Name;
            //createHospital.Code = entity.Hospital.;
            createHospital.City = entity.Hospital.City;
            createHospital.StaffCount = entity.Hospital.StaffCount;
            createHospital.Address = entity.Hospital.DetailedAddress;
            createHospital.DirectAccept = entity.Hospital.DirectAccept;
            createHospital.Remark = entity.Hospital.Remark;

            List<MaterialDemand> MaterialDemands = new List<MaterialDemand>();
            List<Contact> Contacts = new List<Contact>();

            foreach (var item in entity.Contacts)
            {
                Contact contact = new Contact();
                contact.HospitalId = createHospital.Id;
                contact.VolunteerName = item.VolunteerName;
                contact.HospitalContact = item.HospitalContact;
                contact.Phone = item.Phone;
                contact.Remark = item.Remark;
                Contacts.Add(contact);
            }

            foreach (var item in entity.MaterialDemands)
            {
                MaterialDemand demand = new MaterialDemand();
                demand.HospitalId = createHospital.Id;
                demand.DemandName = item.DemandName;
                demand.Quantity = item.Quantity;
                demand.Unit = item.Unit;
                demand.Level = item.Level;
                demand.Remark = item.Remark;
                MaterialDemands.Add(demand);
            }

            createHospital.Contacts = Contacts;
            createHospital.MaterialDemands = MaterialDemands;
            return createHospital;
        }
        public Questionnaire GetQuestionnaireByFormCode(string form_code)
        {
            return _repoQuestionnaire.GetQuestionnaireByFormCode(form_code);
        }
    }
}
